package com.bestcem.xm.user.service.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.user.dao.UserGroupExportRecordDao;
import com.bestcem.xm.user.entity.pojo.UserGroupExportRecordDO;
import com.bestcem.xm.user.service.UserGroupExportRecordService;
import com.bestcem.xm.user.service.dto.PageResponseDTO;
import com.bestcem.xm.user.service.dto.group.UserGroupExportRecordDTO;
import com.bestcem.xm.user.util.convert.UserGroupExportRecordConvert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 层级组织导出service
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/4/19 17:57
 */
@Slf4j
@Service
public class UserGroupExportRecordServiceImpl implements UserGroupExportRecordService {

    @Resource
    private UserGroupExportRecordConvert userGroupExportRecordConvert;

    @Resource
    private UserGroupExportRecordDao userGroupExportRecordDao;

    @Override
    public ServiceResult<Long> countByCondition(UserGroupExportRecordDTO exportRecordDTO) {
        // 校验参数
        if (exportRecordDTO == null) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_LOST);
        }
        if (!ObjectId.isValid(exportRecordDTO.getOrgId())) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_FORMAT);
        }
        // 查询并返回结果
        return ServiceResult.success(userGroupExportRecordDao.countBySelective(userGroupExportRecordConvert.dto2Do(exportRecordDTO)));

    }

    @Override
    public ServiceResult<String> insert(UserGroupExportRecordDTO exportRecordDTO) {
        // 校验参数
        if (exportRecordDTO == null) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_LOST);
        }
        if (!ObjectId.isValid(exportRecordDTO.getOrgId())) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_FORMAT);
        }
        // 新增
        return ServiceResult.success(userGroupExportRecordDao.insertSelective(userGroupExportRecordConvert.dto2Do(exportRecordDTO)));
    }

    @Override
    public ServiceResult<Integer> updateByIdAndOrgId(UserGroupExportRecordDTO exportRecordDTO) {
        // 校验参数
        if (exportRecordDTO == null) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_LOST);
        }
        if (!ObjectId.isValid(exportRecordDTO.getOrgId())) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_FORMAT);
        }
        if (!ObjectId.isValid(exportRecordDTO.getExportId())) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_FORMAT);
        }
        return ServiceResult.success(userGroupExportRecordDao.updateByIdAndOrgId(userGroupExportRecordConvert.dto2Do(exportRecordDTO)));
    }

    @Override
    public ServiceResult<Integer> updateStatus(String orgId, String exportId, Integer status) {
        // 设置参数
        UserGroupExportRecordDTO exportRecordDTO = new UserGroupExportRecordDTO();
        exportRecordDTO.setOrgId(orgId);
        exportRecordDTO.setExportId(exportId);
        exportRecordDTO.setStatus(status);
        return this.updateByIdAndOrgId(exportRecordDTO);
    }

    @Override
    public ServiceResult<PageResponseDTO<UserGroupExportRecordDTO>> listByPage(UserGroupExportRecordDTO exportRecordDTO, Integer currentPage, Integer pageSize) {
        // 查询总条数
        ServiceResult<Long> countRt = this.countByCondition(exportRecordDTO);
        if (!countRt.isSuccess()) {
            return ServiceResult.failWithCode(countRt.getErrorData(), countRt.getMsg());
        }
        PageResponseDTO<UserGroupExportRecordDTO> pageDTO = new PageResponseDTO<>();
        pageDTO.setTotal(countRt.getData());

        if (NumberUtils.LONG_ZERO.compareTo(countRt.getData()) >= 0) {
            pageDTO.setRows(new ArrayList<>());
        } else {
            List<UserGroupExportRecordDO> pageList = userGroupExportRecordDao
                    .selectByPage(userGroupExportRecordConvert.dto2Do(exportRecordDTO), currentPage, pageSize);
            pageDTO.setRows(userGroupExportRecordConvert.dos2Dtos(pageList));
        }
        return ServiceResult.success(pageDTO);
    }

    @Override
    public ServiceResult<List<UserGroupExportRecordDTO>> findByCondition(UserGroupExportRecordDTO exportRecordDTO) {
        // 校验参数
        if (exportRecordDTO == null) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_LOST);
        }
        if (!ObjectId.isValid(exportRecordDTO.getOrgId())) {
            return ServiceResult.failWithCode(ReqErrorCodeEnum.PARAM_FORMAT);
        }
        // 查询并返回结果
        return ServiceResult.success(userGroupExportRecordConvert.dos2Dtos(userGroupExportRecordDao
                .selectBySelective(userGroupExportRecordConvert.dto2Do(exportRecordDTO))));
    }
}
